package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParam;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学员列表分页查询
     * @param studentQueryParam
     * @return
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("学员列表分页查询,studentQueryParam={}", studentQueryParam);
        PageResult pageResult=studentService.pageQuery(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学生,student={}", student);
        studentService.insert(student);
        return Result.success();
    }

    /**
     * 根据ID查询学员
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询学员,id={}", id);
        Student student=studentService.selectById(id);
        return Result.success(student);
    }

    /**
     * 修改学员
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员,student={}", student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 根据id批量删除学员
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("根据id批量删除学员,ids={}", ids);
        studentService.deleteById(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/violation/{id}/{score}")
    public Result addViolation(@PathVariable Integer id,@PathVariable Integer score) {
        log.info("添加学员违规,id={},score={}", id,score);
        studentService.addViolation(id,score);
        return Result.success();
    }
}
